//+------------------------------------------------------------------------------------------------------------------+
#property description                                                                             "[XU-TRADE BALANCE]"
#define Version                                                                                   "[XU-TRADE BALANCE]"
//+------------------------------------------------------------------------------------------------------------------+
#property link        "https://forex-station.com/viewtopic.php?p=1295409935#p1295409935"
#property description "THIS IS A FREE INDICATOR"
#property description "                                                      "
#property description "Welcome to XARD UNIVERSE"
#property description "Let light shine out of darkness and illuminate your world"
#property description "and with this freedom leave behind your cave of denial"
#property indicator_chart_window
string ID;
extern string Indicator              = Version;
//+------------------------------------------------------------------------------------------------------------------+
  extern bool FilterBySymbol         = FALSE,
              FilterByMagicNumber    = FALSE;
   extern int MagicNumber            = 0;
  extern bool CalcSwapCommissionAlso = true;
       string ThousandsSeparator = "'",DecimalSeparator = ".";
          int mCorn = 0, mtextSize = 40, mXDist = 550, mYDist = -10;
        color ProfitColor            = C'0,255,255', LossColor = C'255,225,0', ZeroColor=clrGray;
       string mFont                  = "Digiface";
  extern bool ShowProfitPips         = TRUE;
 extern color PipsColor              = White;
   extern int AddTomYDist            = 20;
       string PipsFont               = "Digiface";
          int PipsTextSize           = 40;  double profitpips;
//+------------------------------------------------------------------------------------------------------------------+
extern string ahis = "******* ALERT SETTINGS:";
 bool   AlertAtProfitOf        = false;
extern double AlertAtProfitAmount    = 1000.00;
 bool   AlertAtLossOf          = false;
extern double AlertAtLossAmount      = 1000.00;
extern int    AlertCandle            = 1;
extern bool   PopupAlerts            = true;
extern bool   EmailAlerts            = false;
extern bool   PushNotificationAlerts = false;
extern bool   SoundAlerts            = false;
extern string SoundFileProfit        = "alert.wav";
extern string SoundFileLoss          = "alert2.wav";
string SoundFileName;
int lastAlert=3;
int Multiplier;
double pips2dbl;
//+------------------------------------------------------------------------------------------------------------------+   
   int init(){ID = "XU TRADE"; IndicatorShortName(ID);
   if(ShowProfitPips) mYDist+=AddTomYDist;
    ObjectCreate("mShowProf", OBJ_LABEL, 0, 0, 0, 0, 0);
       ObjectSet("mShowProf", OBJPROP_CORNER, mCorn);
       ObjectSet("mShowProf", OBJPROP_XDISTANCE, mXDist);
       ObjectSet("mShowProf", OBJPROP_YDISTANCE, mYDist);
       ObjectSet("mShowProf", OBJPROP_BACK, false);
   ObjectSetText("mShowProf", " ", mtextSize, mFont, clrSnow);
   AlertAtLossAmount = MathAbs(AlertAtLossAmount)*(-1);  return(0);}
//+------------------------------------------------------------------------------------------------------------------+
   int start(){
   color mTextCol;
   double mProf = 0;
   profitpips=0.0;
   for(int i = 0; i < OrdersTotal(); i++){
   if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES) == false) break;
   if(FilterBySymbol&&OrderSymbol()!=Symbol()) continue;
   if(FilterByMagicNumber&&OrderMagicNumber()!=MagicNumber) continue;
   if(OrderType()<2){
   if(!CalcSwapCommissionAlso) mProf = mProf + OrderProfit(); else 
   if( CalcSwapCommissionAlso) mProf = mProf + OrderProfit() + OrderSwap() + OrderCommission();
           
   if(ShowProfitPips){BrokerDigitAdjust(OrderSymbol());
   if(OrderType()==OP_BUY) profitpips=profitpips+((MarketInfo(OrderSymbol(),MODE_BID)-OrderOpenPrice())/pips2dbl);
   if(OrderType()==OP_SELL)profitpips=profitpips+((OrderOpenPrice()-MarketInfo(OrderSymbol(),MODE_ASK))/pips2dbl);
   }}}

   //mProf = 18270651.62;//testing
   if(mProf<0.00){mTextCol=LossColor;   SoundFileName=SoundFileLoss;}   else
   if(mProf>0.00){mTextCol=ProfitColor; SoundFileName=SoundFileProfit;} else mTextCol=ZeroColor;
  
   string smProf=DoubleToStr(mProf,2);
   smProf = stringReplace(smProf,".",DecimalSeparator);
   if(mProf>0.00){
   if(lastAlert!=1)
   if(AlertAtProfitOf&&mProf>=AlertAtProfitAmount){
   lastAlert=1; doAlerts("Profit >= "+DoubleToStr(AlertAtProfitAmount,2)+" (current profit: "+DoubleToStr(mProf,2)+") "+AccountCurrency(),SoundFileName); }
   if(mProf>=1000.00&&mProf<10000.00) smProf = StringInsert(smProf,1,ThousandsSeparator);
   if(mProf>=10000.00&&mProf<100000.00) smProf = StringInsert(smProf,2,ThousandsSeparator);
   if(mProf>=100000.00&&mProf<1000000.00) smProf = StringInsert(smProf,3,ThousandsSeparator);
   if(mProf>=1000000.00&&mProf<10000000.00) { smProf = StringInsert(smProf,1,ThousandsSeparator); smProf = StringInsert(smProf,5,ThousandsSeparator); }
   if(mProf>=10000000.00) { smProf = StringInsert(smProf,2,ThousandsSeparator); smProf = StringInsert(smProf,6,ThousandsSeparator); }
   } else if (mProf<0.00) {
   if(lastAlert!=2)
   if(AlertAtLossOf&&mProf<=AlertAtLossAmount){
   lastAlert=2; doAlerts("Profit <= "+DoubleToStr(AlertAtLossAmount,2)+" (current loss: "+DoubleToStr(mProf,2)+") "+AccountCurrency(),SoundFileName); }
   if(MathAbs(mProf)>=1000.00&&MathAbs(mProf)<10000.00) smProf = StringInsert(smProf,2,ThousandsSeparator);
   if(MathAbs(mProf)>=10000.00&&MathAbs(mProf)<100000.00) smProf = StringInsert(smProf,3,ThousandsSeparator);
   if(MathAbs(mProf)>=100000.00&&MathAbs(mProf)<1000000.00) smProf = StringInsert(smProf,4,ThousandsSeparator);
   if(MathAbs(mProf)>=1000000.00&&MathAbs(mProf)<10000000.00) { smProf = StringInsert(smProf,2,ThousandsSeparator); smProf = StringInsert(smProf,6,ThousandsSeparator); }
   if(MathAbs(mProf)>=10000000.00) { smProf = StringInsert(smProf,3,ThousandsSeparator); smProf = StringInsert(smProf,7,ThousandsSeparator);}}
   
                  ObjectSetText("mShowProf",AccountCurrency()+" "+smProf,mtextSize,mFont,mTextCol);
   if(mProf == 0) ObjectSetText("mShowProf","  ",  mtextSize,mFont,mTextCol);//AccountCurrency()+"  "+"0",  mtextSize,mFont,mTextCol);
     
   if(ShowProfitPips) 
   ObjectSetText("mShowProf1","(Profit Pips: "+DoubleToStr(profitpips,2)+")",PipsTextSize,PipsFont,PipsColor);
   
   return(0);}
//+------------------------------------------------------------------------------------------------------------------+
   int deinit(){      ObjectDelete("mShowProf");
   if(ShowProfitPips) ObjectDelete("mShowProf1");  return(0);}
//+------------------------------------------------------------------------------------------------------------------+
//String functions by Hanover, http://www.forexfactory.com/showthread.php?t=165557
//+------------------------------------------------------------------+
string StringInsert(string str1, int pos, string str2)   {
//+------------------------------------------------------------------+
// Creates a result where str2 is inserted into str1, at pos in str1, e.g.
// StringInsert("0123456789",5,"XY") returns "01234XY56789"
   string outstr = StringSubstr(str1,0,pos) + str2 + StringSubstr(str1,pos);  return(outstr);}
//+------------------------------------------------------------------------------------------------------------------+
   string stringReplace(string str, string str1, string str2)  {
//+------------------------------------------------------------------------------------------------------------------+
// Usage: replaces every occurrence of str1 with str2 in str
  string outstr = "";
  for (int i=0; i<StringLen(str); i++)   {
    if (StringSubstr(str,i,StringLen(str1)) == str1)  {
      outstr = outstr + str2;
      i += StringLen(str1) - 1;
    }
    else
      outstr = outstr + StringSubstr(str,i,1);
  }
  return(outstr);}
//+------------------------------------------------------------------------------------------------------------------+
   void doAlerts(string msg,string SoundFile) {
        msg="Show Profit -> Alert on "+Symbol()+", period "+TFtoStr(Period())+": "+msg;
   string emailsubject="MT4 alert on acc. "+AccountNumber()+", "+WindowExpertName()+" - Alert on "+Symbol()+", period "+TFtoStr(Period());
   if (PopupAlerts) Alert(msg);
   if (EmailAlerts) SendMail(emailsubject,msg);
   if (PushNotificationAlerts) SendNotification(msg);
   if (SoundAlerts) PlaySound(SoundFile);}

   string TFtoStr(int period) {
   if (period==0) period=Period();
   switch(period) {
   case 1     : return("M1");  break;
   case 5     : return("M5");  break;
   case 15    : return("M15"); break;
   case 30    : return("M30"); break;
   case 60    : return("H1");  break;
   case 240   : return("H4");  break;
   case 1440  : return("D1");  break;
   case 10080 : return("W1");  break;
   case 43200 : return("MN1"); break;
   default    : return(DoubleToStr(period,0));}  return("UNKNOWN");}
//+------------------------------------------------------------------------------------------------------------------+
   void BrokerDigitAdjust(string symbol){Multiplier = 1;
   if(MarketInfo(symbol,MODE_DIGITS) == 3 || MarketInfo(symbol,MODE_DIGITS) == 5) Multiplier = 10;
   if(MarketInfo(symbol,MODE_DIGITS) == 6) Multiplier = 100;   
   if(MarketInfo(symbol,MODE_DIGITS) == 7) Multiplier = 1000;
      pips2dbl = Multiplier*MarketInfo(symbol,MODE_POINT);}
//+------------------------------------------------------------------------------------------------------------------+